import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class QueryServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String authHeader = request.getHeader("Authorization");

        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            // 创建未授权访问页面
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>未授权访问</title>");
            out.println("<style>");
            out.println("body { font-family: Arial, sans-serif; background-color: #f5f5f5; }");
            out.println(".container { max-width: 800px; margin: 50px auto; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }");
            out.println("h1 { color: #f44336; text-align: center; }");
            out.println(".btn { display: inline-block; padding: 10px 20px; background: #2196F3; color: white; text-decoration: none; border-radius: 4px; margin-top: 20px; }");
            out.println("</style>");
            out.println("</head>");
            out.println("<body>");
            out.println("<div class='container'>");
            out.println("<h1>未授权访问</h1>");
            out.println("<p>您需要提供有效的 JWT Token 才能访问此资源</p>");
            out.println("<a href='index.html' class='btn'>返回登录页面</a>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
            return;
        }

        String token = authHeader.substring(7);
        if (!JwtUtil.validateToken(token)) {
            // 创建无效Token页面
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Token无效</title>");
            out.println("<style>");
            out.println("body { font-family: Arial, sans-serif; background-color: #f5f5f5; }");
            out.println(".container { max-width: 800px; margin: 50px auto; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }");
            out.println("h1 { color: #f44336; text-align: center; }");
            out.println(".btn { display: inline-block; padding: 10px 20px; background: #2196F3; color: white; text-decoration: none; border-radius: 4px; margin-top: 20px; }");
            out.println("</style>");
            out.println("</head>");
            out.println("<body>");
            out.println("<div class='container'>");
            out.println("<h1>Token无效或已过期</h1>");
            out.println("<p>请重新登录获取新的Token</p>");
            out.println("<a href='index.html' class='btn'>返回登录页面</a>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
            return;
        }

        String username = JwtUtil.getUsernameFromToken(token);

        // 创建查询结果页面
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>查询结果</title>");
        out.println("<style>");
        out.println("body { font-family: Arial, sans-serif; background-color: #f5f5f5; }");
        out.println(".container { max-width: 800px; margin: 50px auto; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }");
        out.println("h1 { color: #4CAF50; text-align: center; }");
        out.println(".data-table { width: 100%; border-collapse: collapse; margin: 20px 0; }");
        out.println(".data-table th, .data-table td { border: 1px solid #ddd; padding: 8px; text-align: left; }");
        out.println(".data-table th { background-color: #f2f2f2; }");
        out.println(".data-table tr:nth-child(even) { background-color: #f9f9f9; }");
        out.println(".btn { display: inline-block; padding: 10px 20px; background: #2196F3; color: white; text-decoration: none; border-radius: 4px; margin-top: 10px; }");
        out.println("</style>");
        out.println("</head>");
        out.println("<body>");
        out.println("<div class='container'>");
        out.println("<h1>查询结果</h1>");
        out.println("<p>欢迎, " + username + "! 您的查询结果如下:</p>");

        // 模拟查询结果
        out.println("<table class='data-table'>");
        out.println("<tr><th>ID</th><th>名称</th><th>描述</th><th>创建时间</th></tr>");
        out.println("<tr><td>1001</td><td>项目A</td><td>重要项目</td><td>2023-06-01</td></tr>");
        out.println("<tr><td>1002</td><td>项目B</td><td>紧急项目</td><td>2023-06-15</td></tr>");
        out.println("<tr><td>1003</td><td>项目C</td><td>长期项目</td><td>2023-07-01</td></tr>");
        out.println("</table>");

        out.println("<a href='index.html' class='btn'>返回首页</a>");
        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
    }
}